package ccpvspiral1;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import coalition.CoalitionWithInfoInterface;

import population.Population;
import population.PopulationInterface;

import au.com.bytecode.opencsv.CSVReader;


public class PopulationImporter {
	
	static final int MAX_DISJUNCTIVE_SIZE = 3;

	public static void main(String[] args){
		
		Map<Integer, PopulationInterface> populationsByYear = new HashMap<Integer, PopulationInterface>();
		
		try{
			CSVReader reader = new CSVReader(
					new FileReader(
							"C:\\Users\\leo\\Desktop\\gss.base.usa_7206_summary.csv\\gss.base.usa_7206_summary.csv"));
			
			//String [] nextLine;
			
			TwoWayHashMap<Integer, Integer> raceIndices = new TwoWayHashMap<Integer, Integer>();
			TwoWayHashMap<Integer, Integer> religionIndices = new TwoWayHashMap<Integer, Integer>();
			TwoWayHashMap<Integer, Integer> locationIndices = new TwoWayHashMap<Integer, Integer>();
			int raceIndexCounter = 0;
			int religionIndexCounter = 0;
			int locationIndexCounter = 0;
			
			
			try{
				
				//reader.readNext();
			    List<String[]> entryList = reader.readAll();
			    System.out.println("number of entries to be processed: " + entryList.size());
			    
			    for (String [] nextLine : entryList) {				
			    //while ((nextLine = reader.readNext()) != null) {
			    	
			    	//year,race,relig,location,qty,avg_realinc
			    	//int year = Integer.parseInt(nextLine[0]);
			    	
			    	int year = -1;
			    	try{
			    		year = Integer.parseInt(nextLine[0]);
			    	} catch(NumberFormatException nfe){
			    		continue;
			    	}			    	
			    	
			    	int race = Integer.parseInt(nextLine[1]);
			    	if(raceIndices.getForward(race) == null){
			    		raceIndices.put(race, raceIndexCounter);
			    		raceIndexCounter++;
			    	}
			    	int religion = Integer.parseInt(nextLine[2]);
			    	if(religionIndices.getForward(religion) == null){
			    		religionIndices.put(religion, religionIndexCounter);
			    		religionIndexCounter++;
			    	}			    	
			    	int location = Integer.parseInt(nextLine[3]);
			    	if(locationIndices.getForward(location) == null){
			    		locationIndices.put(location, locationIndexCounter);
			    		locationIndexCounter++;
			    	}				    	
			    	int quantity = Integer.parseInt(nextLine[4]);
			    	float income = Float.parseFloat(nextLine[5]);
			    	
			        // nextLine[] is an array of values from the line
			        System.out.println(nextLine[0] + "; " + nextLine[1] + "; " +  nextLine[2] + "; " + 
			        		nextLine[3] + "; " +  nextLine[4] + "; " +  nextLine[5]);
			    }	
			    
			    System.out.println("raceIndexCounter: " + raceIndexCounter +  
			    		"; religionIndexCounter: " + religionIndexCounter + "" +
			    		"; locationIndexCounter: " + locationIndexCounter);
			    
			    //List<String[]> entryList = reader.readAll();
			    System.out.println("number of entries to be processed: " + entryList.size());
			    
			    for (String[] entry : entryList) {
			    	
			    	//year,race,relig,location,qty,avg_realinc
			    	int year = -1;
			    	try{
			    		year = Integer.parseInt(entry[0]);
			    	} catch(NumberFormatException nfe){
			    		continue;
			    	}
			    	int raceCode = Integer.parseInt(entry[1]);
			    	int race = raceIndices.getForward(raceCode);
			    	int religionCode = Integer.parseInt(entry[2]);
			    	int religion = religionIndices.getForward(religionCode);
			    	int locationCode = Integer.parseInt(entry[3]);
			    	int location = locationIndices.getForward(locationCode);
			    	int quantity = Integer.parseInt(entry[4]);
			    	float income = Float.parseFloat(entry[5]);
			    	
			    	PopulationInterface population = populationsByYear.get(year);
			    	if(population == null){
			    		System.out.println("Adding population for year: " + year);
			    		population = Population.getNewPopulationInterface(
			    				new int[]{ raceIndexCounter + 1, religionIndexCounter + 1, locationIndexCounter + 1});
			    		population.setMaximumDisjunctiveSize(MAX_DISJUNCTIVE_SIZE);
			    		populationsByYear.put(year, population);
			    	}
			    	int[] sectorIndices = new int[]{race, religion, location};
			    	population.setSectorPopulation(sectorIndices, quantity);
			    	population.setSectorPower(sectorIndices, income);
			    	
			    }			    
			    
			} catch(IOException ioe){}
		} catch(FileNotFoundException fnfe){}
		
		Set<Integer> years = populationsByYear.keySet();
		System.out.println("Number of years: " + years.size());
		for(int populationYear : years.toArray(new Integer[]{})){
			PopulationInterface population = populationsByYear.get(populationYear);
			CoalitionWithInfoInterface coalitionWithInfo = population.getWinningCoalition();
			
			System.out.println("Year: " + populationYear);
			System.out.println("Winning Coalition: ");
			System.out.println("size:  " + coalitionWithInfo.getCoalitionSize());
			System.out.println("modulated size:  " + coalitionWithInfo.getCoalitionModulatedSize());
			System.out.println("total population modulated size:  " + population.getTotalModulatedSize());
			System.out.println("fractional power:  " + 
					coalitionWithInfo.getCoalitionModulatedSize()/population.getTotalModulatedSize());
			System.out.println(coalitionWithInfo.getCoalition().toString());			
			
		}
	}
	
}

class TwoWayHashMap<S, T>{
	
	HashMap<S, T> forward = new HashMap<S, T>();
	HashMap<T, S> backward = new HashMap<T, S>();
	
	public T getForward(S s){
		return forward.get(s);
	}
	
	public S getBackward(T t){
		return backward.get(t);
	}	
	
	public void put(S s, T t){
		forward.put(s, t);
		backward.put(t, s);
	}
	
}
